//
//  LHPhotoEnumController.m
//  WXYView
//
//  Created by lehuo on 2017/1/16.
//  Copyright © 2017年 lehuo. All rights reserved.
//

#import "LHPhotoEnumController.h"
#import "LHPhotoChooseController.h"
#import "LHAlertView.h"
#import "GoalData.h"

typedef enum
{
    CameraType,
    AlbumType
}EnumType;//标示当前的照片源是相机还是相册
@interface LHPhotoEnumController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)CameraImage cameraImg;
@property(nonatomic,assign)EnumType enumType;
//判断当前的view是不是第一次显示
@property(nonatomic,assign)BOOL isFirst;
@end

@implementation LHPhotoEnumController

- (instancetype)initWithBlock:(CameraImage)cameraImg
{
    if (self = [super init])
    {
        if (cameraImg)
        {
            self.enumType = CameraType;
            self.cameraImg = cameraImg;
        }else
        {
            self.enumType = AlbumType;
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _isFirst = YES;
   
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.isFirst)
    {
        if(self.enumType == AlbumType) [self showAlbumType];
        else [self showCameraType];
    }
    _isFirst = NO;

}

#pragma mark - 选择图片源为相册时加载
- (void)showAlbumType
{
    self.view.backgroundColor = [UIColor whiteColor];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[LHPhotoChooseController alloc] init]];
    [self addChildViewController:nav];
    [self.view addSubview:nav.view];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pop) name:PostData object:nil];
}

#pragma mark - 选择图片源为相机时加载
- (void)showCameraType
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
        pickerVC.allowsEditing = YES;
        pickerVC.delegate = self;
        [self presentViewController:pickerVC animated:YES completion:nil];
    }else
    {
        [LHAlertView showSimpleInfo:@"你丫没有相机，用毛啊！"];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}



- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//接到通知，销毁控制器
- (void)pop
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"info = %@",info);
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *img = info[UIImagePickerControllerOriginalImage];
    self.cameraImg(img);
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
